package cn.myspringboot.articletype.controller;

import cn.myspringboot.anno.PreAuthorize;
import cn.myspringboot.articletype.service.ArticleTypeService;
import cn.myspringboot.user.domain.ArticleType;
import cn.myspringboot.user.query.ArticleTypeQuery;
import cn.myspringboot.utils.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@Api("文章类型的接口")
@RestController
@RequestMapping("/articleType")
public class ArticleTypeController {
    @Autowired
    private ArticleTypeService articleTypeService;

    @PreAuthorize(name = "添加或修改文章类型信息",sn = "articleType:add",descs ="添加或修改文章类型信息")
    @ApiOperation("添加或修改文章类型信息")
    @PostMapping("add")
    public R add(@RequestBody ArticleType articleType) {
        if(articleType.getId() == null){
            articleType.setCreateTime(new Date());
            articleType.setUpdateTime(new Date());
            articleTypeService.insert(articleType);
        }else {
            articleType.setUpdateTime(new Date());
            articleTypeService.update(articleType);
        }
        return R.ok();
    }

    @PreAuthorize(name = "根据id删除文章类型信息",sn = "articleType:delete",descs ="根据id删除文章类型信息")
    @ApiOperation("根据id删除文章类型信息")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") Long id) {
        articleTypeService.delete(id);
        return R.ok();
    }

    @PreAuthorize(name = "批量删除文章类型信息",sn = "articleType:deleteBatch",descs ="批量删除文章类型信息")
    @ApiOperation("批量删除文章类型信息")
    @PostMapping("/deleteBatch")
    public R deleteBatch(@RequestBody List<Long> ids) {
        articleTypeService.batchdelete(ids);
        return R.ok();
    }

    @PreAuthorize(name = "根据id查询文章类型信息",sn = "articleType:getById",descs ="根据id查询文章类型信息")
    @ApiOperation("根据id查询文章类型信息")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id) {
        ArticleType byId = articleTypeService.findById(id);
        return R.ok(byId);
    }

    @PreAuthorize(name = "分页查询文章类型信息",sn = "articleType:page",descs ="分页查询文章类型信息")
    @ApiOperation("分页查询文章类型信息")
    @PostMapping("/page")
    public R page(@RequestBody ArticleTypeQuery query) {
        PageHelper.startPage(query.getPage(),query.getSize());
        List<ArticleType> page = articleTypeService.getArticleTypeByPage(query);
        PageInfo<ArticleType> pageInfo = new PageInfo<>(page);
        return R.ok(pageInfo);
    }

    @PreAuthorize(name = "查询所有文章类型信息",sn = "articleType:add",descs ="查询所有文章类型信息")
    @ApiOperation("查询所有文章类型信息")
    @GetMapping
    public R getAll() {
        List<ArticleType> all = articleTypeService.findAll();
        return R.ok(all);
    }

    @PreAuthorize(name = "查询文章中所有类型",sn = "articleType:articleTypeTree",descs ="查询文章中所有类型")
    @ApiOperation("查询文章中所有类型")
    @GetMapping("/articleTypeTree")
    public R articleTypeTree() {
        return R.ok(articleTypeService.articleTypeTree());
    }
}
